package com.ngkj.zmfptback.sys.controller;

import com.ngkj.zmfptCommon.common.bean.CommonReslut;
import com.ngkj.zmfptCommon.common.bean.Role;
import com.ngkj.zmfptCommon.common.util.ResultEnum;
import com.ngkj.zmfptback.business.controller.TenxunApi;
import com.ngkj.zmfptback.sys.bean.RealUserEntity;
import com.ngkj.zmfptback.sys.mapper.read.UserRoleReadMapper;
import com.ngkj.zmfptback.sys.mapper.write.UserRoleWriteMapper;
import com.ngkj.zmfptback.sys.service.read.RealUserReadService;
import com.ngkj.zmfptback.sys.service.read.RoleReadService;
import com.ngkj.zmfptback.sys.service.read.UserReadService;
import com.ngkj.zmfptback.sys.service.write.RealUserWriteService;
import com.ngkj.zmfptback.sys.service.write.UserWriteService;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author ChenHN
 * @since 2022/1/9 creation
 **/
@RestController
@RequestMapping("/realUser")
public class RealUserController {

    @Resource
    private UserReadService userReadService;

    @Resource
    private UserWriteService userWriteService;

    @Resource
    private RealUserReadService realUserReadService;

    @Resource
    private RealUserWriteService realUserWriteService;

    @Resource
    private RoleReadService roleReadService;

    @Resource
    private UserRoleWriteMapper userRoleWriteMapper;

    @Resource
    private TenxunApi tenxunApi;


    @GetMapping("/getRealUserInfo")
    public CommonReslut getRealUserInfo(Long userId) {
        try {
            Long realUserId = userReadService.getRealUserIdByUserId(userId);
            if (realUserId != null) {
                RealUserEntity realUser = realUserReadService.getRealUserInfo(realUserId);
                return new CommonReslut(ResultEnum.ok, realUser);
            } else {
                return new CommonReslut(ResultEnum.ok);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new CommonReslut(ResultEnum.error);
        }
    }


    @PostMapping("/updateRealUser")
    public CommonReslut updateRealUser(@RequestBody RealUserEntity realUser) {
        try {
            Integer id = realUser.getId();
            if (id == null) {
                realUserWriteService.addRealUser(realUser);
                userWriteService.updateRealId(realUser.getUserId(), realUser.getId());
                String roleIdByRoleCode = roleReadService.getRoleIdByRoleCode(Role.MEMBER);
                List<String> roleList = new ArrayList<>();
                roleList.add(roleIdByRoleCode);
                userRoleWriteMapper.addUserRoles(realUser.getUserId(), roleList);
            } else {
                realUserWriteService.updateRealUser(realUser);
            }
            return new CommonReslut(ResultEnum.ok, realUser);
        } catch (Exception e) {
            e.printStackTrace();
            return new CommonReslut(ResultEnum.error);
        }
    }

    /**
     * 收费结果码：
     * 0: 姓名和身份证号一致
     * -1: 姓名和身份证号不一致
     * 不收费结果码：
     * -2: 非法身份证号（长度、校验位等不正确）
     * -3: 非法姓名（长度、格式等不正确）
     * -4: 证件库服务异常
     * -5: 证件库中无此身份证记录
     *
     * @param name
     * @param idCard
     * @return
     */
    @GetMapping("/verifyIdCard")
    public CommonReslut<String> verifyIdCard(String name, String idCard) {
//        调用人身核验api 收费
//        String response = tenxunApi.verifyIdCard(name, idCard);
        return new CommonReslut(ResultEnum.ok, 0);
//        Object response = null;
//        if (StringUtils.isEmpty(response)) {
//            return new CommonReslut(ResultEnum.error, null);
//        } else {
//            return new CommonReslut(ResultEnum.ok, response);
//        }
    }


    @GetMapping("/validateMoblie")
    public CommonReslut<String> validateMoblie(String name, String moblie) {
        //        调用人身核验api 收费
//        String response = tenxunApi.validateMoblie(name,moblie);
        return new CommonReslut(ResultEnum.ok, 0);
//        Object response = null;
//        if (StringUtils.isEmpty(response)) {
//            return new CommonReslut(ResultEnum.error, null);
//        } else {
//            return new CommonReslut(ResultEnum.ok, response);
//        }
    }


}
